
from flask_migrate import Migrate
from app import create_app
from app.extensions import db

app = create_app()
migrate = Migrate(app, db)
import click
from werkzeug.security import generate_password_hash

@app.cli.command("bootstrap-data")
def bootstrap_data():
    """Create admin user and seed default categories."""
    from app.models import AdminUser, Category  # ← 放在函数里再导入，避免循环
    # 管理员
    if not AdminUser.query.filter_by(username='admin').first():
        u = AdminUser(username='admin', password_hash=generate_password_hash('admin123'))
        db.session.add(u)
    # 分类
    names = ['玉镯','吊坠挂饰','手链项链','戒指扳指','耳饰']
    orders = { '玉镯':10, '吊坠挂饰':20, '手链项链':30, '戒指扳指':40, '耳饰':50 }
    for n in names:
        c = Category.query.filter_by(name=n).first()
        if not c:
            c = Category(name=n, sort_order=orders[n])
            c.set_slug()
            db.session.add(c)
    db.session.commit()
    click.echo('初始化完成：管理员 admin/admin123 + 5 个分类')



if __name__ == '__main__':
    app.run()